<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/home' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 操作区域 -->
    <div style="margin: 0 0 20px 0;">
      <el-button type="primary" size="medium" @click="$router.push('/service/order/add')">添加预约订单</el-button>


      <el-input v-model="searchUsername" placeholder="请输入用户关键字"
                 style="width: 250px;position: relative;left: 20px"></el-input>
      <el-button @click="searchUsername1" type="primary" style="position: relative;left: 20px">搜索</el-button>

      <el-input v-model="searchDoctorName" placeholder="请输入医生关键字"
                style="width: 250px;position: relative;left: 40px"></el-input>
      <el-button @click="searchDoctor1" type="primary" style="position: relative;left: 40px">搜索</el-button>

      <template >
        <el-select v-model="value" placeholder="选择状态" style="position: relative;left: 60px" @change="selectState()">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              >
          </el-option>
        </el-select>
      </template>
    </div>
    <!-- 数据表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120" align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="phoneNumber" label="电话"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="appointment" label="预约时间"  align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="description" label="预约情况" align="center"
                       :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="doctorName" label="预约医生" width="100" align="center"></el-table-column>
      <el-table-column prop="state" label="预约状态" width="120" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控件 -->
    <div style="margin: 10px 0; text-align: right;">
      <el-pagination
          @current-change="changePage"
          :hide-on-single-page="true"
          :page-size="pageSize"
          :current-page="currentPage"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>

    <!-- 修改预约订单的弹出表单Dialog -->
    <el-dialog title="辑相订单" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="editForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input v-model="editForm.phoneNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="预约时间" :label-width="formLabelWidth">
          <el-input v-model="editForm.appointment" autocomplete="off"></el-input>
        </el-form-item>
      <el-form-item label="预约简介" :label-width="formLabelWidth">
        <el-input v-model="editForm.description" autocomplete="off"></el-input>
      </el-form-item>

        <el-form-item label="预约状态" :label-width="formLabelWidth">
          <el-input v-model="editForm.state" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="预约医生" :label-width="formLabelWidth">
          <el-input v-model="editForm.doctorName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 根据用户名关键字查找预约订单信息 -->
    <el-dialog title="订单(username)" :visible.sync="dialogByUsernameFormVisible" style="width: 100%">
      <!-- 数据表格 -->
      <el-table :data="editFormByUsername" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
        <el-table-column prop="username" label="名称" width="120" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="phoneNumber" label="电话" width="120" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="appointment" label="预约时间" width="200" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="description" label="预约情况" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="doctorName" label="预约医生" width="100" align="center"></el-table-column>
        <el-table-column prop="state" label="预约状态" width="120" align="center"></el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="openEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 根据医生关键字查找预约订单信息 -->
    <el-dialog title="订单(doctorname)" :visible.sync="dialogByDoctorNameFormVisible" style="width: 100%">

      <!-- 数据表格 -->
      <el-table :data="editFormByDoctorName" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
        <el-table-column prop="doctorName" label="预约医生" width="100" align="center"></el-table-column>
        <el-table-column prop="username" label="用户名" width="120" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="phoneNumber" label="电话" width="120" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="appointment" label="预约时间" width="200" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>

        <el-table-column prop="description" label="预约情况" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>


        <el-table-column prop="state" label="预约状态" width="120" align="center"></el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="openEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      stateStr:"",
      value:"",
      options: [{
        value: '0',
        label: '过期订单'
      }, {
        value: '1',
        label: '正常预约'
      }, {
        value: '2',
        label: '进行中订单'
      }, {
        value: '3',
        label: '已完成订单'
      }],
      searchUsername:"",
      searchDoctorName:"",
      // 表格数据
      tableData: [],
      // 分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      pageSize: 20,
      pageCount: 1,
      total: 0,
      // 编辑对话框的可见性
      dialogFormVisible: false,
      dialogByUsernameFormVisible:false,
      dialogByDoctorNameFormVisible:false,
      // 编辑表单
      editFormByUsername:[],
      editFormByDoctorName:[],
      editForm: {
        id: '',
        username: '',
        phoneNumber:"",
        description: '2022/12/12 12:12:30',
        state:"",
        doctorName:""
      },
      // 编辑表单的label宽度
      formLabelWidth: '120px'
    }
  },
  methods: {
    selectState(){

      let url = 'http://localhost:9999/order/listByState?state='+this.value;
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          // .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOrderList();
            }
          });
        }
      });

    },
    // 根据用户名关键字查询预约订单
    searchUsername1() {
      let url = 'http://localhost:9999/order/listByUsername?username='+this.searchUsername;
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log("-----------------------------------------------------------------")
        console.log(jsonResult);
        if (jsonResult.state == 20000) {
          this.dialogByUsernameFormVisible = true;
          this.editFormByUsername = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOrderList();
            }
          });
        }
      });
    },

// 根据医生姓名关键字查询预约订单
    searchDoctor1() {
      let url = 'http://localhost:9999/order/listByDoctor?doctorName='+this.searchDoctorName;
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log("----------------------------------------")
        console.log(jsonResult)
        if (jsonResult.state == 20000) {
          this.dialogByDoctorNameFormVisible = true;
          this.editFormByDoctorName = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOrderList();
            }
          });
        }
      });
    },



    // 打开编辑对话框
    openEditDialog(order) {
      let url = 'http://localhost:9999/order/' + order.id;
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.dialogFormVisible = true;
          this.editForm = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOrderList();
            }
          });
        }
      });
    },
    // 提交编辑表单
    submitEditForm() {
      let url = 'http://localhost:9999/order/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改相册成功！',
            type: 'success'
          });
          this.loadOrderList();
          this.dialogFormVisible = false;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadOrderList();
            }
          });
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
    // 打开删除确认框
    openDeleteConfirm(order) {
      let message = '此操作将永久删除【' + order.name + '】相册，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(order);
      }).catch(() => {
      });
    },
    // 处理删除
    handleDelete(order) {
      let url = 'http://localhost:9999/order/' + order.id + '/delete';
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除相册成功！',
            type: 'success'
          });
          this.loadOrderList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOrderList();
            }
          });
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
    // 切换分页
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadOrderList();
    },
    // 加载相册列表
    loadOrderList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9999/order?page=' + page;
      console.log('url = ' + url);

      let loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
      this.axios
          .create({'headers': {'Authorization': loginInfo.token}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.pageCount = jsonResult.data.maxPage;
          this.total = jsonResult.data.total;
          this.stateStr = this.tableData.state;
          // if(this.tableData.state==="0"){
          //   this.tableData.state=""
          // }

        }
      });
    }
  },
  mounted() {
    this.loadOrderList();
  }
}
</script>

<style scoped>

</style>
